################################
### SBV structures & classes ###
################################

class sbv_event:

	time_start = "00:00:00.000"
	time_end = "00:00:00.000"
	text = []

class sbv:

	events = []

	def merge_event(self, event):
		# FIXME: each insert needs to be verified, if the event timespan overlaps with existing timespans
		# if yes a resampling is required
		self.events.append(event)

	def load(self, filename):
		print("Loading: '" + filename + "'")
		filestream = open(filename, 'r')
		for line in file:
			event = sbv_event()
			event.text = [] # NOTE: event.text has value from previous loop iteration, so we need to reset it
			line = strip_line(line)
			# read timespan
			event.time_start = line.split(",")[0].zfill(12)
			event.time_end = line.split(",")[1].zfill(12)
			line = strip_line(filestream.next())
			# read text
			while (line):
				event.text.append(line)
				try:
					line = strip_line(filestream.next())
				except StopIteration:
					break
			self.events.append(event)
		filestream.close()

	def save(self, filename):
		advanced = 0
		print("Saving: '" + filename + "'")
		filestream = open(filename, 'w')
		for event in self.events:
			if (len(event.text) > 0):
				if (advanced):
					filestream.write(event.time_start.replace(",", ".") + "," + event.time_end.replace(",", ".") + newline)
				else:
					filestream.write(event.time_start[1:].replace(",", ".") + "," + event.time_end[1:].replace(",", ".") + newline)
				for line in event.text:
					if (line):
						filestream.write(line + newline)
				filestream.write(newline)
		filestream.close()
